package com.weiai.wedding.service.cases;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.weiai.wedding.dao.BaseQueryDao;
import com.weiai.wedding.dao.cases.ResourcesDao;
import com.weiai.wedding.entity.cases.Resources;
import com.weiai.wedding.service.ShiroDbRealm;
import com.weiai.wedding.util.ConUtils;
import com.weiai.wedding.util.Constants;

/**
 * @author wangweiheng
 *
 */
@Component
@Transactional(readOnly=true)
public class ResourcesService {
	
	private static Logger logger = LoggerFactory.getLogger(ResourcesService.class);
	@Autowired
	private ResourcesDao resourcesDao;
	@Autowired
	private BaseQueryDao base;
	@Autowired
	private ShiroDbRealm shiroRealm;
	/**
	 * 保存资源
	 * @param resources 资源实体
	 * @param fileName 文件名称
	 * @param inputStream 图片流
	 */
	@Transactional(readOnly=false)
	public void saveResources(Resources resources,String fileName,InputStream inputStream){
		String path = Constants.DIR+File.separator+Constants.RESOURCES
			+File.separator+ConUtils.formatDate(new Date(),"yyMMdd")
			+File.separator;
		ConUtils.createFileForInputStream(path, fileName, inputStream);
		resources.setResourcesDir(path+fileName);
		resources.setCreateTime(ConUtils.getDate());
		resources.setStatus("A");
		resources.setCreateBy(ConUtils.getUser().getLoginName());
		resourcesDao.save(resources);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}
	/**
	 * 修改资源
	 * @param resources 资源实体
	 * @param fileName 文件名称
	 * @param inputStream 图片流
	 */
	@Transactional(readOnly=false)
	public void updateResources(Resources resources,String fileName,InputStream inputStream){
		Resources po = resourcesDao.findOne(resources.getResourcesId());
		String path = Constants.DIR+File.separator+Constants.RESOURCES
		+File.separator+resources.getResourcesName()+ConUtils.formatDate(new Date(),"yyMMddHHmmssssss")
		+File.separator;
		String oldPath = po.getResourcesDir().substring(0,po.getResourcesDir().indexOf(po.getResourcesName()+17));
		resources.setCreateBy(po.getCreateBy());
		resources.setCreateTime(po.getCreateTime());
		resources.setStatus(po.getStatus());
		ConUtils.createFileForInputStream(path, fileName, inputStream);
		resources.setResourcesDir(path+fileName);
		resourcesDao.save(resources);
		
		File file = new File(oldPath);
		ConUtils.deleteFile(file);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}
	
	/**
	 * 删除资源
	 * @param id
	 */
	@Transactional(readOnly=false)
	public void deleteResources(Long id){
		Resources resources = resourcesDao.findOne(id);
		resources.setStatus("X");//删除状态
		resourcesDao.save(resources);
	}
	
	/**
	 * 查询所有的资源信息
	 * @return
	 */
	public Page<Resources> getAll(Pageable pager){
		return resourcesDao.findResourcesList(pager);
	}
	
	/**
	 * 根据资源名称查询所有的资源信息
	 * @param resourcesName
	 * @return
	 */
	public Page<Resources> getAllByName(String resourcesName,Pageable pager){
		return resourcesDao.findResourcesListByName("%"+resourcesName+"%",pager);
	}
	
	/**
	 * 根据ID查询详细信息
	 * @param id
	 * @return
	 */
	public Resources getResources(Long id){
		
		return resourcesDao.findOne(id);
	}
	
	
}
